package org.budo.support.assertion;

import java.util.Collection;
import java.util.Map;

import org.budo.support.lang.util.StringUtil;

/**
 * @author lmw
 */
public class Assert {
    public static <T> T notNull(T value, String message) {
        if (null == value) {
            throw new AssertNotNullException(message);
        }

        return value;
    }

    public static String notEmpty(String value, String message) {
        if (null == value || value.isEmpty()) {
            throw new AssertStringNotEmptyException(message);
        }

        return value;
    }

    public static Collection notEmpty(Collection collection, String message) {
        if (null == collection || collection.isEmpty()) {
            throw new AssertCollectionNotEmptyException(message);
        }

        return collection;
    }

    public static Map notEmpty(Map map, String message) {
        if (null == map || map.isEmpty()) {
            throw new AssertMapNotEmptyException(message);
        }

        return map;
    }

    public static Object[] notEmpty(Object[] array, String message) {
        if (null == array || array.length == 0) {
            throw new AssertArrayNotEmptyException(message);
        }

        return array;
    }

    public static String[] arrayLength(String[] array, int length, String message) {
        if (null == array || array.length != length) {
            throw new AssertArrayLengthException(message);
        }

        return array;
    }

    /**
     * 传入的字符串全都不是空的
     */
    public static String[] notAnyEmpty(String[] array, String errorMessage) {
        if (null == array || array.length < 1) {
            throw new AssertException(errorMessage);
        }
        for (String each : array) {
            if (StringUtil.isEmpty(each)) {
                throw new AssertException(errorMessage);
            }
        }
        return array;
    }
}